package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    MongoTemplate mongoTemplate;
    
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        //判断当前是否有账户登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user != null){
            Integer ApUserid = AppThreadLocalUtils.getUser().getId();
            //根据id查询实体对象
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(ApUserid, null);
            //根据实体id,文章id设置点赞
            likeBehavior(String.valueOf(apBehaviorEntry.getId()),dto);
            //返回响应
            return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else {
            if (dto.getEquipmentId() != null){  //设备账号登录
                //查询设备实体
//                Query query = Query.query(Criteria.where("refId").is(dto.getEquipmentId()).and("type").is(0));
//                ApBehaviorEntry apBehaviorEntry = mongoTemplate.findOne(query, ApBehaviorEntry.class);
                ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(null, dto.getEquipmentId());
                //根据实体id,文章id设置点赞
                likeBehavior(apBehaviorEntry.getId(),dto);
                //返回响应
                return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }else {
                log.error("点赞功能异常,检测不到登录用户或设备id");
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN, "参数不合格");
    }

    private void likeBehavior(String id, LikesBehaviorDTO dto) {
        //设置查询条件
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId())
                .and("type").is(dto.getType()).and("operation").is(0));
        //判断点赞状态
        if ( dto.getOperation() == 0){ //点赞

            ApLikesBehavior behavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (behavior == null){
                //插入一条点赞数据
                ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
                apLikesBehavior.setArticleId(dto.getArticleId());
                apLikesBehavior.setCreatedTime(new Date());
                apLikesBehavior.setEntryId(String.valueOf(id));
                apLikesBehavior.setOperation(dto.getOperation());
                apLikesBehavior.setType((short)0);
                mongoTemplate.save(apLikesBehavior);
            }else {
                log.error("请勿重复点赞");
                CustException.cust(AppHttpCodeEnum.DATA_EXIST);
            }
        }else if (dto.getOperation() == 1 ){    //取消点赞
            //根据实体id跟文章Id,删除改数据
            mongoTemplate.remove(query,ApLikesBehavior.class);
        }
    }
}
